function addCustomItemsValue(valueMap, customAttrcode, defaultValue) {
    const customValueMap = {};
    if (valueMap[customAttrcode]) {
        const { value } = valueMap[customAttrcode];
        if (typeof value === 'object' && value instanceof Object) {
            Object.keys(value).forEach(key => {
                customValueMap[`custom_${customAttrcode}_${key}`] = {
                    display: value[key],
                    value: value[key],  
                };
            });
        }
    } else {
        customValueMap[`custom_${customAttrcode}_default`] = {
            display: defaultValue,
            value: defaultValue,  
        }; 
    }
    return customValueMap
}

function addCustomTemplateItems(props, valueMap, customAttrcode, customLabel) {
    const { form, metas, formCode } = props;
    let meta = metas.getMeta();
    let template = meta[formCode];
    let templateItems = template.items || [];
    templateItems = templateItems.filter(item => item.attrcode.indexOf(`custom_${customAttrcode}`) === -1);
    template.items = [...getCustomTemplateItems(templateItems, valueMap, customAttrcode, customLabel)];
    metas.setMeta(meta, () => {
        form.updateForm(formCode);
    });
}

function getCustomTemplateItems(srcTemplateItems, valueMap, customAttrcode, customLabel) {
    const templateItems = [...srcTemplateItems];
    if (valueMap[customAttrcode]) {
        const { value } = valueMap[customAttrcode];
        if (typeof value === 'object' && value instanceof Object) {
            Object.keys(value).sort(vacationquotaSort).forEach((key, index) => {
                templateItems.push(
                    Object.assign(getVacationquotaTemplateItem(), {
                        isnextrow: (index < 1),
                        attrcode: `custom_${customAttrcode}_${key}`,
                        label: `${key}${customLabel}`,
                        position: templateItems.length + 1,
                    })
                )
            });
        }
    } else {
        templateItems.push(
            Object.assign(getVacationquotaTemplateItem(), {
                attrcode: `custom_${customAttrcode}_default`,
                label: customLabel,
                position: templateItems.length + 1,
            })
        )
    }
    return templateItems;
}

function delCustomTemplateItems(props, customAttrcode) {
    const { form, metas, formCode } = props;
    let meta = metas.getMeta();
    let template = meta[formCode];
    let templateItems = template.items || [];
    template.items = templateItems.filter(item => item.attrcode.indexOf(`custom_${customAttrcode}`) === -1);
    metas.setMeta(meta, () => {
        form.updateForm(formCode);
    });
}

function vacationquotaSort(a, b) {
    return new Date(a).getTime() - new Date(b).getTime();
}

function getVacationquotaTemplateItem() {
    return {
        col: '6',
        colnum: '1',
        isnextrow: true,
        attrcode: 'vacationquota',
        color: '#555555',
        disabled: true,
        itemtype: 'input',
        label: '可休额度',
        position: 14,
        required: false,
        visible: true,
    };
}

export {
    addCustomItemsValue,
    addCustomTemplateItems,
    delCustomTemplateItems,
}
